<template>
	<view class="product_details" :class='equipment'>
		<app-title title='商品详情'></app-title>
		<view class="product_details_box">
			<view class="product_details_box_top">
				<image :src="res.thumb" mode=""></image>
			</view>
			<view class="product_details_box_title">
				<view class="product_details_box_title_name">
					{{res.title}}
				</view>
				<view class="product_details_box_title_marketprice">
					<p>￥{{res.marketprice}}</p>
					<view class="">
						999+人付款
					</view>
				</view>
			</view>
			<view class="product_details_box_commodity_details">
				<uv-divider text="商品详情" textColor="#ff0000" lineColor="#ff0000"></uv-divider>
				<view class="product_details_box_commodity_details_box" v-html='res.content'>

				</view>
			</view>
			<view class="product_details_box_goumai">
				<view class="product_details_box_goumai_box" @click="openFun">
					立即购买
				</view>
			</view>
		</view>

		<uv-popup ref="popup" mode="bottom">
			<view class="popup_box">
				<view class="popup_box_goods">
					<image :src="res.thumb" mode=""></image>
					<view class="popup_box_goods_text">
						<p>{{marketprice?marketprice:res.marketprice}}</p>
					</view>
				</view>
				<view class="number">
					<p>数量：</p>
					<uv-number-box v-model="num" @change="valChange"></uv-number-box>
				</view>
				<view class="options">
					<view v-for="(item,index) in optionlist" :key='index' class="optionlist">
						<view style="line-height: 80upx;" v-if="item.title">{{item.title}}</view>
						<view class="filter-body-section-body style-flex style-flex-wrap">
							<axb-check-box ref="radio3" :list="item.list"
								@change="radioChangeType($event,item.id)"></axb-check-box>
						</view>
					</view>
				</view>
				<view class="popup_box_goumai">
					<view class="product_details_box_goumai_box" @click="goumaiFun">
						立即购买
					</view>
				</view>
			</view>
		</uv-popup>





	</view>
</template>

<script>
	import {
		goods_detail,
		goods_picker
	} from '@/api/home.js';

	import axbCheckBox from '@/components/axb-checkbox/axb-checkbox.vue';
	export default {
		components: {
			axbCheckBox
		},
		data() {
			return {
				openid: '',
				res: '',
				MultipleSpecifications: '',
				show: false,
				num: 1,
				id: '',
				optionid: '',
				ress: '',
				maxbuy: '',
				options: '',
				optionlist: '',
				classes: [],
				marketprice: '',
				equipment: ''

			};
		},
		onLoad(e) {
			this.id = e.id;
			if (process.env.UNI_PLATFORM === 'h5') {
				// 在H5环境中打开
				console.log('在H5环境中打开');
				this.equipment = 'H5'
			} else if (process.env.UNI_PLATFORM === 'app-plus') {
				// 在App环境中打开
				console.log('在App环境中打开');
				this.equipment = 'App'
			}
			uni.getStorage({
				key: 'openid',
				success: (res) => {
					this.openid = res.data;
					this.goods_detailFun({
						openid: res.data,
						id: e.id
					});
				}
			});
		},
		methods: {
			openFun() {
				this.$refs.popup.open();
			},
			radioChangeType(val, id) { //选择规格
				console.log(val, id);
				id = id + ''
				if (val == null) {
					val = ''
				}
				this.classes[id] = val
				let list = this.optionlist[0].list;
				for (var i = 0; i < list.length; i++) {
					if (list[i].value == val) {
						this.marketprice = list[i].marketprice
					}
				}
			},
			valChange(e) {

			},
			goumaiFun() {
				var specs = '',
					optionid = 0
				for (var i in this.classes) {
					specs = specs + '_' + this.classes[i]
				}
				for (var j in this.options.options) {
					if ('_' + this.options.options[j].specs == specs) {
						optionid = this.options.options[j].id
					}
				}

				if (this.MultipleSpecifications.options) {
					if (optionid === 0) {
						this.showinteraction('请选择规格');
					} else {
						this.show = !this.show;
						uni.navigateTo({
							url: `/pages/home/confirmAnOrder/confirmAnOrder?id=${this.id}&total=${this.num}&optionid=${optionid}`
						})
					}
				} else {
					this.show = !this.show;
					uni.navigateTo({
						url: `/pages/home/confirmAnOrder/confirmAnOrder?id=${this.id}&total=${this.num}`
					})
				}
			},
			open() {},
			close() {
				this.show = false
			},
			//提示
			showinteraction(title, icon = 'none') {
				uni.showToast({
					title,
					icon,
					duration: 2000,
					mask: false
				})
			},
			async goods_detailFun(up) {
				let data = await goods_detail(up);
				let datas = await goods_picker(up);
				this.MultipleSpecifications = datas.result;
				console.log(datas);
				let content = data.content + '';
				content = content.replace(/data-lazy/g, "src");
				content = content.replace(/ /g, ' style="width:100%;display:block;"');
				data.content = content;
				this.res = data;



				if (datas.result.goods.maxbuy == 0) {
					this.maxbuy = Number(datas.result.goods.total)
				} else {
					this.maxbuy = Number(datas.result.goods.maxbuy)
				}
				if (datas.result.goods.minbuy != 0) {
					var minbuy = Number(datas.result.goods.minbuy)
					this.minbuy = minbuy
					this.numvalue = minbuy
					this.total = minbuy
				}
				this.options = datas.result
				var arry = []
				for (var i in this.options.specs) {
					var a = []
					var v = {
						id: '',
						list: [],
						title: ''
					}
					for (var j in this.options.specs[i].items) {
						var ary = {
							name: '',
							value: '',
							checked: 0,
						}
						ary['name'] = this.options.specs[i].items[j].title
						ary['value'] = this.options.specs[i].items[j].id
						ary['marketprice'] = this.options.specs[i].items[j].price
						a.push(ary)
					}
					v['list'] = a
					v['id'] = this.options.specs[i].id
					v['title'] = this.options.specs[i].title
					arry.push(v)
				}
				this.optionlist = arry;
				this.ress = data;
				console.log(this.optionlist)
			},
		}
	}
</script>

<style lang="scss">
	.popup_box {
		width: 100%;
		min-height: 400rpx;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		position: relative;
		overflow: visible;

		.popup_box_goods {
			padding: 20rpx;
			width: 100%;
			display: flex;
			align-items: flex-end;
			position: absolute;
			top: -50rpx;
			border-bottom: 2rpx solid #dfdfdf;
			z-index: 1000;


			image {
				background-color: #fff;
				width: 150rpx;
				height: 150rpx;
				box-shadow: 0 0 20rpx #ccc;
			}



			.popup_box_goods_text {
				width: 300rpx;
				height: 152rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				padding: 0 0 0 20rpx;


				span {
					font-size: 30rpx;
					font-weight: 600;
					text-wrap: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				p {
					width: 300rpx;
					text-wrap: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					color: #ff1200;
				}
			}
		}

		.number {
			margin-top: 150rpx;
			display: flex;
			padding: 0 20rpx;
			justify-content: space-between;
			align-items: center;
		}

		.options {
			width: 100%;
			// display: flex;
			max-height: 400rpx;
			padding: 0 20rpx;
			font-size: 26rpx;
			overflow: auto;
			padding: 20rpx;

			.options_item {
				border: 1rpx solid #F1F1F1;
				padding: 10rpx 20rpx;
				margin-right: 20rpx;
			}

		}

		.product_details_box_goumai_box {
			width: 100%;
			height: 80rpx;
			// background: linear-gradient(3deg, #ff3232, #ff3232, #ff8719);\
			background-color: #FF6000;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #F2F2F2;
		}


	}


	.product_details {
		width: 100%;
		display: flex;
		// padding-top: var(--status-bar-height);
		flex-direction: column;

		.product_details_box {
			width: 100%;
			flex: 1;
			padding-top: 20rpx;
			overflow-y: auto;

			.product_details_box_top {
				width: 100%;
				height: 450rpx;
				display: flex;
				justify-content: center;

				image {
					width: 450rpx;
					height: 450rpx;
				}
			}

			.product_details_box_title {
				width: 690rpx;
				min-height: 192rpx;
				background: #fff;
				box-shadow: 0px 0px 20rpx 0px #F2F2F2;
				border-radius: 20rpx;
				margin: 10rpx auto;
				position: relative;
				z-index: 1;
				padding: 30rpx;

				.product_details_box_title_name {
					width: 100%;
					color: #2b2f38;
				}

				.product_details_box_title_marketprice {
					padding: 20rpx 0;
					font-size: 26rpx;
					display: flex;
					justify-content: space-between;
					color: #b0b0b0;

					p {
						color: #ff1200;
					}
				}
			}

			.product_details_box_commodity_details {
				width: 100%;
				padding: 0 40rpx;
				margin-bottom: 140rpx;

				.product_details_box_commodity_details_box {}
			}

			.product_details_box_goumai {
				width: 100%;
				height: 130rpx;
				background-color: #fff;
				position: fixed;
				bottom: 0;
				padding: 20rpx 30rpx;

				.product_details_box_goumai_box {
					width: 100%;
					height: 100%;
					// background: linear-gradient(3deg, #ff3232, #ff3232, #ff8719);
					background-color: #FF6000;
					border-radius: 100rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #F2F2F2;
				}
			}


		}
	}
</style>